/***********************
产品型号：YMC13265-13
IC:ST7567	4SPI
VLCD=3.3V,VLCD=9.4V,1/65DUTY,1/9BIAS
***********************/
#include "main.h"
#include "ST7567.h"


sbit VDD = P4 ^ 1;
sbit VLED = P4 ^ 3;
sbit key = P1 ^ 7;

unsigned char getKey(void);
unsigned char keyState = 0;
unsigned char rxd = 0;
static unsigned char powerState = 0;

void pwrOff();
void pwrOn();


void main()
{
	//delay_us(0);
	delay_ms(10);
	delay_ss(0);
	
	uartInitial();

	delay_ms(10);
	pwrOn();
	
	delay_ms(30);
	hwReset();
	
	ST7567_Init();
	line(0xff);icon(0x00);
	line(0x00);icon(0x00);
	
	pwrOff();
	
	
	while(1)
	{
		keyState = getKey();
		
		if(keyState == 0){
			delay_ms(20);
			if(getKey() == 0) {
				while(getKey() == 0);
				
				if(powerState == 0) {
					pwrOn();
	
					delay_ms(30);
					hwReset();
					
					ST7567_Init();
					line(0xff);icon(0xff);
					line(0x00);icon(0x00);
					
					SendData(0x3f);
				} else {
					line(0x00);icon(0x00);
					P10 = 0;
					pwrOff();
					SendData(0x3e);
				}
				
				
			}
		}
		
		if(isRxFlag()){
			clearRxFlag();
			rxd = SBUF;
			switch(rxd){
				case(0x30):
				line(0x00);icon(0x00);
				break;
				
				case(0x31):
				line(0x55);icon(0xff);
				break;
				
				case(0x32):
				line(0xaa);icon(0x00);
				break;
				
				case(0x33):
				ver(0xff,0x00,0xff,0x00);
				break;
				
				case(0x34):
				ver(0x00,0xff,0x00,0xff);
				break;
				
				case(0x35):	
				chess8(0);
				break;
				
				case(0x36):			
				chess8(1);
				break;
				
				case(0x37):
				cross(0);
				break;
				
				case(0x38):
				cross(1);
				break;
				
				case(0x39):
				delay_us(0);
				SendString("test uart ok\r\n");
				lcdTest();
				break;
				
				case(0x3e):
					line(0x00);icon(0x00);
					P10 = 0;
					pwrOff();
				break;
				
				case(0x3f):
				line(0xff);icon(0xff);
				break;
				
				default:
					SendData(0xff);
				break;
			}
			SendData(rxd);
		}
	}
}

void pwrOn()
{
	VLED = 0;
	VDD = 0;
	powerState = 1;
}

void pwrOff()
{
	VLED = 1;
	VDD = 1;
	powerState = 0;
}

unsigned char getKey(void){
	return key;
}
